package com.qingcloud.adminbackend.controller;


import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.sysfunction.*;
import com.qingcloud.adminbackend.entity.sysfunction.*;
import com.qingcloud.adminbackend.entity.sysorg.SysOrgLogoDTO;
import com.qingcloud.adminbackend.service.ISysFunctionService;
import com.qingcloud.adminbackend.utils.CommonUtil;
import com.qingcloud.base.annotation.LoginUser;
import com.qingcloud.base.remote.pojo.LoginUserDTO;
import com.qingcloud.base.remote.pojo.MenuDTO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统功能表 前端控制器
 * </p>
 *
 * @author liyuhong
 * @since 2018-11-16
 */
@RestController
@RequestMapping("/sys-function")
public class FunctionController {

    @Resource
    private ISysFunctionService sysFunctionService;

    @ApiOperation(value = "获取功能权限", notes = "通过用户ID获取功能权限信息", response = MenuDTO.class)
    @PostMapping("/getFunctionByUserId")
    public AjaxResult getFunctionByUserId(@RequestBody SysFunctionDTO sysFunctionDTO,
                                          HttpServletRequest request, @LoginUser LoginUserDTO loginUserDTO) {
        String userId = loginUserDTO.getUserId();
        boolean isAdmin = loginUserDTO.getIsAdmin()==1?true:false;

        if (CommonUtil.isEmpty(sysFunctionDTO.getUserId())) {
            sysFunctionDTO.setUserId(userId);
        }
        if (!StringUtils.isEmpty(sysFunctionDTO.getOrderIds()) && sysFunctionDTO.getOrderIds().size() == 0) {
            return new AjaxResult(WamingConstant.SUCCESS, null, new ArrayList<>());
        }
        return new AjaxResult(WamingConstant.SUCCESS, null, sysFunctionService.getFunctionByUserId(isAdmin, sysFunctionDTO));
    }

    @ApiOperation(value = "根据业务种类获取菜单树", notes = "根据业务种类获取菜单树", response = MenuDTO.class)
    @ApiImplicitParam(name = "businessType", value = "业务种类编号", dataTypeClass = Integer.class, paramType = "path")
    @GetMapping("/business/{businessType}")
    public AjaxResult findByBusinessType(@PathVariable("businessType") Integer businessType) {
        final SysFunctionDTO sysFunctionDTO = new SysFunctionDTO();
        sysFunctionDTO.setMenuType(businessType.toString());
        return new AjaxResult(WamingConstant.SUCCESS, null,
                sysFunctionService.getFunctionByUserId(true, sysFunctionDTO));
    }

    @ApiOperation(value = "获取模块", notes = "通过用户ID获取模块信息", response = ModuleDTO.class)
    @GetMapping("/getModuleByUserId")
    public AjaxResult getModuleByUserId(String menuTypes) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysFunctionService.getModuleByUserId(menuTypes));
    }

    @ApiOperation(value = "根据客户ID获取所属功能", notes = "根据客户ID获取所属功能")
    @PostMapping("/getFunctionByMenuBO")
    public AjaxResult getFunctionByMenuBO(@RequestBody MenuBO menuBO) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysFunctionService.getFunctionByMenuBO(menuBO));
    }

    @ApiOperation(value = "新增用户授权功能列表", notes = "新增用户授权功能列表", response = InitFunctionTreeDTO.class)
    @PostMapping("/getUserAuthFunctions")
    public AjaxResult getUserAuthFunctions(@RequestBody FunctionParam param, HttpServletRequest request, @LoginUser LoginUserDTO loginUserDTO) {
        String userId = loginUserDTO.getUserId();
        boolean isAdmin = loginUserDTO.getIsAdmin()==1?true:false;
        return new AjaxResult(WamingConstant.SUCCESS, "", sysFunctionService.getUserAuthFunctions(param.getRoleIds(), param.getOrgIds(), userId, param.getMenuType(),
                isAdmin));
    }

    @ApiOperation(value = "登录用户的权限功能", notes = "登录用户的权限功能")
    @Cacheable(value = "func", key ="#loginUserDTO.userId+'-'+#menuType")
    @GetMapping("/getUserAuthFunctions/{menuType}")
    public AjaxResult getLoginUserAllFunctions(@PathVariable("menuType") Integer menuType, HttpServletRequest request, @LoginUser LoginUserDTO loginUserDTO) {
        String userId = loginUserDTO.getUserId();
        return new AjaxResult(WamingConstant.SUCCESS, "", sysFunctionService.getAllFunctionCode(String.valueOf(menuType),userId));
    }

    @ApiOperation(value = "登录用户所有的功能权限", notes = "登录用户所有的功能权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuType", value = "应用类型", defaultValue = "1", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", defaultValue = "1", required = true, dataType = "String", paramType = "query")
    })
    @Cacheable(value = "func", key ="#userId+'-'+#menuType")
    @GetMapping("/getAllFunction")
    public AjaxResult getAllFunction(@RequestParam(value = "menuType") String menuType,
                                     @RequestParam(value = "userId") String userId) {
        List<String> functionCodeList = sysFunctionService.getAllFunctionCode(menuType, userId);
        return new AjaxResult(WamingConstant.SUCCESS, "", functionCodeList);
    }

    @ApiOperation(value = "登录用户有权限的菜单树", notes = "登录用户有权限的菜单树")
    @GetMapping("/getMenuTree/{menuType}")
    public AjaxResult getMenuTree(@PathVariable("menuType") Integer menuType, HttpServletRequest request, @LoginUser LoginUserDTO loginUserDTO) {
        String userId = loginUserDTO.getUserId();
        boolean admin = loginUserDTO.getIsAdmin()==1?true:false;
        return new AjaxResult(WamingConstant.SUCCESS, "", sysFunctionService.getMenuTree(userId, menuType, admin));
    }

    @ApiOperation(value = "登录用户所有的菜单权限", notes = "登录用户所有的菜单权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuType", value = "1:网格化菜单；2：健康云菜单；3：权限管理", defaultValue = "1", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "urlFlag", value = "menuType为1时，一级菜单Url返回标识 0不返回(默认) 1返回", dataType = "int", paramType = "query",
                    defaultValue = "0"),
            @ApiImplicitParam(name = "userId", value = "用户ID", defaultValue = "1", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isAdmin", value = "是否是管理员", defaultValue = "true", required = true, dataType = "boolean", paramType = "query")
    })
    @GetMapping("/getAllMenu")
    public AjaxResult getAllMenu(@RequestParam(value = "menuType") String menuType,
                                 @RequestParam(value = "urlFlag") Integer urlFlag,
                                 @RequestParam(value = "userId") String userId,
                                 @RequestParam(value = "isAdmin") Boolean isAdmin) {

        List<MenuDTO> allMenu = sysFunctionService.getAllMenu(menuType, urlFlag, userId);
        return new AjaxResult(WamingConstant.SUCCESS, "", allMenu);
    }

    @ApiOperation(value = "获取业务类型对应的Logo", notes = "获取业务类型对应的Logo", response = SysOrgLogoDTO.class)
    @GetMapping("/getOrgLogo")
    public AjaxResult getOrgLogo(@RequestParam(value = "businessTypes") String businessTypes,
                                 @RequestParam(value = "orgId") String orgId) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysFunctionService.getOrgLogo(businessTypes, orgId));
    }

}

